package ru.amse.tsyganov.jumleditor.view;

import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.graphics.Rectangle;

import ru.amse.tsyganov.jumleditor.graphicaleditor.Coordinates;
import ru.amse.tsyganov.jumleditor.model.State;
import ru.amse.tsyganov.jumleditor.view.preferences.Colors;
import ru.amse.tsyganov.jumleditor.view.representation.StateView;

public class ConnectionPoint {
	
	private static final int SIZE = 4;
	
	public static enum ConnectionPointType {
		LEFT, RIGHT, TOP, BOTTOM;
	}
	
	private ConnectionPointType type;
	
	private final StateView<? extends State> container;
	
	private PositionKeeper positionKeeper;
	
	private final int number;
	
	private boolean selected;
	
	public ConnectionPoint(ConnectionPointType type, 
			StateView<? extends State> container,
			PositionKeeper positionKeeper,
			int number) {
	
		this(type, container, number);
		this.positionKeeper = positionKeeper;
	}
	
	public ConnectionPoint(ConnectionPointType type, 
			StateView<? extends State> container,
			int number) {
		
		this.type = type;
		this.container = container;
		this.number = number;
	}

	public int getCpX() {
		return positionKeeper != null ? positionKeeper.getPosX() : 0;
	}
	
	public int getCpY() {
		return positionKeeper != null ? positionKeeper.getPosY() : 0;
	}
	
	public void paint(GC gc, Coordinates coordinates) {
		gc.setForeground(Colors.getBlueColor());
		gc.setBackground(Colors.getBlueColor());
		Rectangle r = getRectangle();
		
		int margin = selected ? 1 : 0;
		
		gc.fillRectangle(
			coordinates.fromModelXToScreenX(r.x - margin),
			coordinates.fromModelYToScreenY(r.y - margin),
			r.width + 2 * margin + 1, r.height + 2 * margin + 1);
	}
	
	public Rectangle getRectangle() {
		return new Rectangle(getCpX() - SIZE / 2 + 1, 
				getCpY() - SIZE / 2 + 1, SIZE - 2, SIZE - 2);
	}

	public StateView<? extends State> getContainer() {
		return container;
	}

	public ConnectionPointType getType() {
		return type;
	}

	public void setType(ConnectionPointType type) {
		this.type = type;
	}

	public void setPositionKeeper(PositionKeeper positionKeeper) {
		this.positionKeeper = positionKeeper;
	}

	public Integer getNumber() {
		return number;
	}

	public void setSelected(boolean selected) {
		this.selected = selected;
	}
}
